

cc.Class({
    extends: cc.Component,

    properties: {
        m_Hero: cc.Animation,
        m_BtnRoll: cc.Button,
        m_background1: [cc.Node],
        m_background2: [cc.Node],
        m_background3: [cc.Node],
        m_background4: [cc.Node],
        m_background5: [cc.Node],
        m_floor: [cc.Node],
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        // var anm =  
        this.initView();

    },
    initView() {
        this.playAnimtion('Run');
        this.m_BtnRoll.node.on(cc.Node.EventType.TOUCH_START, this.rollStart, this);
        this.m_BtnRoll.node.on(cc.Node.EventType.TOUCH_END, this.rollEnd, this);
        this.m_BtnRoll.node.on(cc.Node.EventType.TOUCH_CANCEL, this.rollEnd, this);

        var tempArray = [this.m_background1, this.m_background2, this.m_background3, this.m_background4, this.m_background5, this.m_floor];
        var times = [M_background1_move_Time, M_background2_move_Time, M_background3_move_Time, M_background4_move_Time, M_background5_move_Time, M_f_move_Time1];

        for (let j = 0; j < tempArray.length; j++) {
            //初始化位置
            for (let i = 0; i < tempArray[j].length; i++) {
                var back_node = tempArray[j][i];
                var width = back_node.width;
                back_node.setPosition(i * (width - 3), 0);
                var move = cc.moveTo(times[j] * (i + 1), -(width - 3), 0);
                var seq = cc.sequence(move, cc.callFunc(this.backMoveEnd, this, times[j]));
                back_node.runAction(seq);
            }

        }



        // //初始化位置
        // for (let i = 0; i < this.m_background2.length; i++) {
        //     var back_node =  this.m_background2[i];
        //     var width = back_node.width;
        //     back_node.setPosition(i*(width -3),0);
        //     var move = cc.moveTo(M_background2_move_Time*(i+1),-(width -3),0);
        //     var seq = cc.sequence(move,cc.callFunc(this.backMoveEnd,this,M_background2_move_Time));
        //     back_node.runAction(seq);
        //    }
        //    for (let i = 0; i < this.m_background3.length; i++) {
        //     var back_node =  this.m_background3[i];
        //     var width = back_node.width;
        //     back_node.setPosition(i*(width -3),0);
        //     var move = cc.moveTo(M_background3_move_Time*(i+1),-(width -3),0);
        //     var seq = cc.sequence(move,cc.callFunc(this.backMoveEnd,this,M_background3_move_Time));
        //     back_node.runAction(seq);
        //    }
        //    for (let i = 0; i < this.m_background4.length; i++) {
        //     var back_node =  this.m_background4[i];
        //     var width = back_node.width;
        //     back_node.setPosition(i*(width -3),0);
        //     var move = cc.moveTo(M_background4_move_Time*(i+1),-(width -3),0);
        //     var seq = cc.sequence(move,cc.callFunc(this.backMoveEnd,this,M_background4_move_Time));
        //     back_node.runAction(seq);
        //    }
    },
    backMoveEnd(target, data) {

        var width = target.width;
        target.setPosition((width - 3), 0);
        var move = cc.moveTo(data * 2, -(width - 3), 0);
        var seq = cc.sequence(move, cc.callFunc(this.backMoveEnd, this, data));

        target.runAction(seq);
    },
    rollStart() {
        if (this.m_Hero.currentClip.name == 'Jump') {
            return;
        }
        this.playAnimtion('Roll');
    },
    rollEnd() {
        if (this.m_Hero.currentClip.name == 'Jump') {
            return;
        }
        this.playAnimtion('Run');
    },
    onChangeAnimation(target, data) {

        if (data == 'Jump' && this.isChangeClip('Jump')) {

            var moveUp = cc.moveTo(1, -95, 40).easing(cc.easeCubicActionOut());
            var moveDown = cc.moveTo(1, -95, -50).easing(cc.easeCubicActionIn());
            var callback = cc.callFunc(this.callbackDownOver, this.m_Hero, this);
            var seq = cc.sequence(moveUp, moveDown, callback);
            this.m_Hero.node.runAction(seq);

        }
        this.playAnimtion(data);
    },
    callbackDownOver() {
        cc.log("动画执行完毕");
        var anm = this.getComponent(cc.Animation);
        anm.play('Run');
    },
    //播放动画
    playAnimtion(playname) {

        if (this.isChangeClip(playname) == false) {
            return;
        }
        cc.log("正在执行========1===" + playname);
        if (playname == 'Run') {
            this.m_Hero.node.setPosition(-95, -50);
        } else if (playname == 'Roll') {

            this.m_Hero.node.setPosition(-95, -58);
        }
        this.m_Hero.play(playname);
    },
    //是否可以切换动画
    isChangeClip(playname) {

        // if(this.m_Hero.currentClip.name == 'Run'){
        //     return true;
        // }
        // return false;
        if (playname == 'Roll') {
            if (this.m_Hero.currentClip.name == 'Jump') {

                return false;
            } else if (this.m_Hero.currentClip.name == 'Run') {

                return true;
            }

        } else if (playname == 'Jump') {
            if (this.m_Hero.currentClip.name == 'Run') {
                return true;
            } else {
                return false;
            }
        }
        return true;
    },
    // update (dt) {},
});
